<?php
class Admin_Model_User extends Core_Database
{
      /**
     * Lay toan bo email cua cac thanh vien
     * 
     * @param : void
     * @return: array(email) : neu ton tai    null : neu khong ton tai
     * */
   public function getAllEmail()
   {
     $sql = 'select email from users';
     $sth = $this->_db->prepare($sql);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
   }
   
    /**
     * Lay danh sach tat ca cac user
     * @param: void
     * @return : array(): neu co it nhat mot user  null: neu khong co user nao
     * 
     * */
    public function getUsers()
    {
     $sql = 'select * from users';
     $sth = $this->_db->prepare($sql);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;   
    }
    
       /**
     * Them tai khoan
     * true : thanh cong
     * false: that bai
     * 
     * @param :username password fullname email credit ban user_dir
     * @return :bool 
     */
    public function insertUser($username,$password,$fullname,$email,$credit,$ban,$user_dir)
    {
    	$sql = 'insert into users(username,password,fullname,email,credit,ban,user_dir) values (:username,PASSWORD(:password),:fullname,:email,:credit,:ban,:user_dir)';
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':username', $username, PDO::PARAM_STR);
    	$sth->bindParam(':password', $password, PDO::PARAM_STR);
    	$sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
    	$sth->bindParam(':email', $email, PDO::PARAM_STR);
    	$sth->bindParam(':credit', $credit, PDO::PARAM_INT);
    	$sth->bindParam(':ban', $ban, PDO::PARAM_INT);
    	$sth->bindParam(':user_dir', $user_dir, PDO::PARAM_STR);
    	return $sth->execute();
    }
    
 	/**
    * Lay thong tin cua mot user
    * 
    * @param : user_id
    * @return : array : neu ton tai  null : neu khong ton tai
    * 
    * */
    public function getUser($user_id)
    {
     $sql = 'select * from users where user_id = :user_id';
     $sth = $this->_db->prepare($sql);
     $sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
     $status = $sth->execute();
     if (!$status)
          return null;
     $result = $sth->fetchAll();
     if(count($result)>0)
        return $result;
     else
        return null;     
    }  
    
 	 /**
     * Doi thong tin cua user
     * @param : user_id  , fullname, credit, ban, password
     * @return : true : thanh cong , false: that bai
     * */
     public function updateUser($user_id,$fullname,$credit,$ban,$password)
     {
     	if($password != "")
     		$sqlpass = ", password = PASSWORD(:password) ";
     	else 
     	    $sqlpass = "";	
        $sql = 'update users set fullname = :fullname, credit = :credit, ban = :ban '.$sqlpass 
                .' where user_id = :user_id';
        
    	$sth = $this->_db->prepare($sql);
    	$sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
    	
    	
  	    $sth->bindParam(':fullname', $fullname, PDO::PARAM_STR);
  	    $sth->bindParam(':credit', $credit, PDO::PARAM_INT);
  	    $sth->bindParam(':ban', $ban, PDO::PARAM_INT);
  	    if($password != "")
     		$sth->bindParam(':password', $password, PDO::PARAM_STR);	
    	return $sth->execute();
     }
     
     /**
      * Xoa mot user
      * @param :user_id
      * @return bool  true: thanh cong false: that bai
      * */
      public function deleteUser($user_id)
      {
        $sql = 'delete from users where user_id = :user_id';
        $sth = $this->_db->prepare($sql);
        $sth->bindParam(':user_id',$user_id,PDO::PARAM_INT);
        return $sth->execute();
      } 
    
}